import { _decorator, Animation, Button, Color, Component, EditBox, error, instantiate, JsonAsset, Label, Layout, Node, Prefab, resources, ScrollView, Sprite, sys, UITransform, Vec2, VerticalTextAlignment } from 'cc';
import { TileData } from './TileData';
const { ccclass, property } = _decorator;

const userDataKey = 'userData';

interface MapItemData {
    name: string,
    type: string,
    color: string,
    resources: string[],
    monsters: string[],
    buildings: [],
    events: [],
    rules: any,
    position: { x: number, y: number }
}

interface UserData {
    name: string
    race: string
    startPoint: string
    curMap: string
    position: { x: number, y: number }
}
//
interface MapData {
    name: string
    tiles: MapItemData[]
}
@ccclass('miniMap')
export class miniMap extends Component {

    @property(ScrollView)
    mapViewNode: ScrollView = null;
    @property(Prefab)
    mapUILabel: Prefab = null;

    @property(Node)
    miniMapNode: Node = null;

    private mapItemData: MapItemData[] = [];

    private playPosCor: Node = null;
    private playPos: { x: number, y: number } = { x: 0, y: 0 };
    //获取玩家所在的地图
    loadUserData() {

        const jsonStr = sys.localStorage.getItem(userDataKey);
        const userData = JSON.parse(jsonStr) as UserData;
        return userData;
    }
    //加载
    loadMapData() {

        const jsonStr = sys.localStorage.getItem(this.loadUserData().startPoint + 'MinimapData');
        const mapData = JSON.parse(jsonStr) as MapData;
        return mapData;
    }
    //加载
    loadWanjiaCurMap(path: string) {
        resources.load(path, JsonAsset, (error, jsonAsset) => {
            if (error) {
                console.error(error);
                return;
            }
            const wanJiaCurMap = jsonAsset.json;
        })
    }
    //地图要显示玩家所在位置
    //

    start() {
        this.generateMiniMapUI();
        this.paintAndSrollToPlay();
    }

    update(deltaTime: number) {

    }

    protected onDisable(): void {
        // this.miniMapNode.children.forEach(child => child.destroy());
    }

    protected onEnable(): void {
        this.paintAndSrollToPlay();
    }

    paintAndSrollToPlay() {
        this.miniMapNode.children.forEach(i => {
            if (i.getComponent(TileData).positionX === this.loadUserData().position.x && i.getComponent(TileData).positionY === this.loadUserData().position.y) {
                i.getChildByName('Sprite').active = true;
                // i.getChildByName('Sprite').getComponent(Animation).play();
            }else{
                i.getChildByName('Sprite').active = false;
            }
        })
        console.log('标记并滚动到位置');
        // this.playPosCor.getComponent(Sprite).color = new Color().fromHEX("#FF9999");
        this.mapViewNode.scrollTo(new Vec2(this.playPos.x, this.playPos.y));
    }
    //生成小地图
    generateMiniMapUI() {
        this.mapItemData = this.loadMapData().tiles;
        const mapWidth = Math.sqrt(this.mapItemData.length);

        for (const tile of this.mapItemData) {
            const mapLabel = instantiate(this.mapUILabel);
            mapLabel.getComponent(Sprite).color = new Color().fromHEX(tile.color).multiply(new Color().fromHEX("#FAF9DE"));
            mapLabel.getChildByName('Label').getComponent(Label).string = tile.name;
            mapLabel.getComponent(TileData).positionX = tile.position.x;
            mapLabel.getComponent(TileData).positionY = tile.position.y;
            // 检查当前tile是否是玩家所在位置
            const userData = this.loadUserData();
            if (tile.position.x === userData.position.x && tile.position.y === userData.position.y) {
                // mapLabel.getComponent(Sprite).color = new Color().fromHEX("#FF9999");
                const posx = parseFloat((tile.position.x / mapWidth).toFixed(1));
                const posy = parseFloat((tile.position.y / mapWidth).toFixed(1));
                this.playPos = { x: posx, y: posy };
            }
            this.miniMapNode.getComponent(Layout).constraintNum = mapWidth;
            this.miniMapNode.addChild(mapLabel);
        }

        //更新小地图大小
        this.miniMapNode.getComponent(UITransform).width = mapWidth * 50;
        this.miniMapNode.getComponent(UITransform).height = mapWidth * 50;
        this.miniMapNode.getParent().getComponent(UITransform).width = mapWidth * 50;
        this.miniMapNode.getParent().getComponent(UITransform).height = mapWidth * 50;
    }

}


